//
//  HandleView.m
//  CarLifes
//
//  Created by yu on 2017/7/3.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "HandleView.h"

@implementation HandleView

- (instancetype)init {
    if (self = [super init]) {
        
        if (@available(iOS 13.0, *)) {
            self.backgroundColor = [UIColor tertiarySystemBackgroundColor];
        } else {
            self.backgroundColor = [UIColor whiteColor];
        }
        
        _playBtn = [self buttonWithTitle:@"" imageName:@"play" selectImageName:@"pause" sel:@selector(playAction:)];
        [self addSubview:_playBtn];
        
        // 进度
        _progrSlider = [[UISlider alloc] init];
        [_progrSlider addTarget:self action:@selector(progress:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_progrSlider];
        
        // 速度
        _speedSlider = [[UISlider alloc] init];
        _speedSlider.continuous = NO;
        [_speedSlider addTarget:self action:@selector(speed:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_speedSlider];
        
        UILabel *ratioLabel = [self titleLabelWithText:@"进度" font:14];
        [self addSubview:ratioLabel];
        
        UILabel *speedLabel = [self titleLabelWithText:@"速度" font:14];
        [self addSubview:speedLabel];
        
#pragma mark - AutoLayout -
        _playBtn.sd_layout
        .leftSpaceToView(self, 10)
        .centerYEqualToView(self)
        .widthIs(35)
        .heightEqualToWidth();
        
        ratioLabel.sd_layout
        .rightSpaceToView(self, 10)
        .topSpaceToView(self, 20)
        .heightIs(20);
        [ratioLabel setSingleLineAutoResizeWithMaxWidth:50];
        
        speedLabel.sd_layout
        .rightEqualToView(ratioLabel)
        .bottomSpaceToView(self, 20)
        .heightRatioToView(ratioLabel, 1);
        [speedLabel setSingleLineAutoResizeWithMaxWidth:50];
        
        _progrSlider.sd_layout
        .leftSpaceToView(_playBtn, 10)
        .rightSpaceToView(ratioLabel, 10)
        .topEqualToView(ratioLabel)
        .heightRatioToView(ratioLabel, 1);
        
        _speedSlider.sd_layout
        .centerYEqualToView(speedLabel)
        .leftEqualToView(_progrSlider)
        .rightSpaceToView(speedLabel, 10)
        .heightRatioToView(_progrSlider, 1);
        
    }
    return self;
}

- (void)playAction:(UIButton *)btn {
    if (_playBlock) {
        _playBlock(btn);
    }
}

- (void)progress:(UISlider *)slider {
    if (_progrBlock) {
        _progrBlock(slider.value);
    }
}

- (void)speed:(UISlider *)slider {
    if (_speedBlock) {
        _speedBlock(slider.value);
    }
}

- (UILabel *)titleLabelWithText:(NSString *)text font:(CGFloat)font {
    UILabel *titleLabel = [UILabel new];
//    titleLabel.backgroundColor = [UIColor cyanColor];
    titleLabel.text = text;
    titleLabel.font = SYSTEM_FONT(font);
    titleLabel.textColor = kTextBlackColor;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    return titleLabel;
}

- (UIButton *)buttonWithTitle:(NSString *)title imageName:(NSString *)imageName selectImageName:(NSString *)selectImageName sel:(SEL)sel {
    UIButton *button = [UIButton new];
    [button setTitle:title forState:UIControlStateNormal];
//    button.backgroundColor = [UIColor cyanColor];
    [button setTitleColor:kTextBlackColor forState:UIControlStateNormal];
    [button setImage:IMAGE_NAMED(imageName) forState:UIControlStateNormal];
    [button setImage:IMAGE_NAMED(selectImageName) forState:UIControlStateSelected];
    [button addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    return button;
}

@end
